# Author: Gregory Smith
# Date: 12 - 3 - 2018
# Title: 2018 Secret But Constrained Final Texreg Presentation Script  
# Job: Creates Latex Code for the Models in the Paper and Appendix 

# The file smith-2019-covert-final-tidy-output-functions needs to run for the 
#   following code to work 

#---- Packages

#---- Packages 

list.of.packages <- c("tidyverse")

new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

library(tidyverse)

#----- Loads in Tidy Model Data Frames 

# Steps:
# 1) Specfifies a file path 
# 2) Finds all file names in that path that end in .csv  
# 3) Adds variable that includes the file name of each model (uses file extension)
# 4) Removes ".csv" from each file name and creates a model name column 
# 5) Makes distinct data frames for each model 


#-------- Loads in Tidy Model Output and GOF Statistics into two tibbles 

model_path <- "smith-2019-tidy-model-output/"
model_files <- dir(path = model_path, pattern = "*.csv")

model_dta <- data_frame(filename = model_files) %>% # create a data frame
  mutate(tidy_models = map(filename, ~ read_csv(file.path(model_path, .)))) %>% 
  mutate(filename = str_sub(filename, end = -13)) %>% 
  dplyr::rename(model_name = filename)

# glimpse(model_dta)

gof_path <- "smith-2019-gof-statistics/"
gof_files <- dir(path = gof_path, pattern = "*.csv")

gof_dta <- data_frame(filename = gof_files) %>% # create a data frame
  mutate(gof_stats = map(filename, ~ read_csv(file.path(gof_path, .)))) %>% 
  mutate(filename = str_sub(filename, end = -9)) %>% 
  dplyr::rename(model_name = filename)

# glimpse(gof_dta)

#------- Merges the model_dta and gof_dta tibbles together 

final_dta <- model_dta %>% left_join(gof_dta, by = "model_name")
remove(model_dta, gof_dta)


#---- Reiterates the function 

index_id <- 1:nrow(final_dta)
lapply(index_id, tex_reg_create)


#----- Creates the Texreg Tables in the Paper 

#----- Covert Operations: O'Rourke & Pooled Samples 

# $^{*}$ p < 0.10, $^{**}$ p < 0.05, $^{***}$ p < 0.01.

texreg(list(divided_orke_base.tr, divided_orke_full.tr, orke_divided_interaction.tr,
            divided_cmb_base.tr, divided_cmb_full.tr, cmb_divided_interaction.tr),
       #stars = c(0.01,  0.05, 0.10),
       float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "oversight_divided_interact" = "Oversight Interaction"),
       caption = "The Determinants of US Covert Operations",
       custom.model.names = c("O'Rourke 1",  "O'Rourke 2",  "O'Rourke 3",
                              "Pooled 1",  "Pooled 2", "Pooled 3"),
      custom.note = "$^*$ 0 outside the confidence interval. 
             Peace year terms have been suppressed but are displayed in the appendix.
             Models O'Rourke 1 and Pooled 1 contain presidential fixed effects.",
       sideways = TRUE
) 

# Peace year terms have been suppressed but are displayed in the appendix.


#------------ Major and Minor GML MIDs 

texreg(list(minor_us_base_mid_div.tr, minor_us_mid_div.tr,
            major_us_base_mid_div.tr, major_us_mid_div.tr),
       float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of Minor US Initiated MIDs",
       custom.model.names = c("Minor MID 1", "Minor MID 2",
                              "Major MID 1",  "Major MID 2"),
       custom.note = "$^*$ 0 outside the confidence interval. 
       Peace year terms have been suppressed but are displayed in the appendix.
       Every model contains presidential fixed effects.", 
       sideways = TRUE
)


#----- Models Included in the Appendix

#----- Covert Operations: O'Rourke Sample 

screenreg(list(divided_orke_base.tr, divided_orke_full.tr, 
            majority_orke_base.tr, majority_orke_full.tr, 
            lppc_orke_base.tr, lppc_orke_full.tr),
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "orourke_peace_years" = "Peace Years",
                              "orourke_peace_years_2" = "Peace Years Squared",
                              "orourke_peace_years_3" = "Peace Years Cubed"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of US Covert Operations: O'Rourke Sample",
       label = "table:appendix_orourke_sample",
       #custom.model.names = c("O'Rourke 1",  "O'Rourke 2", "O'Rourke 3",
       #                       "Pooled 1",  "Pooled 2", "Pooled 3"), # "Oversight Int."),
       custom.note = "$^*$ 0 outside the confidence interval. 
       Models 1, 3, and 5 contain presidential fixed effects.",
       sideways = TRUE)


#----- Covert Operations: Pooled Sample 

texreg(list(divided_cmb_base.tr, divided_cmb_full.tr,
            majority_cmb_base.tr, majority_cmb_full.tr, 
            lppc_cmb_base.tr, lppc_cmb_full.tr),
       #ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
     #  use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       # omit.coef = "ears",
       #reorder.coef = c(2, 3, 13, 9, 12, 7, 11, 4, 8, 10, 5, 6, 1),
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "us_peace_years" = "Peace Years",
                              "us_peace_years_2" = "Peace Years Squared",
                              "us_peace_years_3" = "Peace Years Cubed"),
       #  "(Intercept)" = "Intercept"),
       caption = "The Determinants of US Covert Operations: Pooled Sample",
       label = "table:appendix_pooled_sample",
       # custom.model.names = c("Base Unif ", "Full Unif",
       #                       "Base Maj", "Full Maj",
       #                      "Base LPPC", "Full LPPC"), # , "Oversight Int."),
       custom.note = "$^*$ 0 outside the confidence interval. 
       Models 1, 3, and 5 contain presidential fixed effects.",
       sideways = TRUE
)


#------------ Minor US Initiated GML MIDs

texreg(list(minor_us_base_mid_div.tr, minor_us_mid_div.tr,
            minor_us_base_mid_mjt.tr, minor_us_mid_mjt.tr, 
            minor_us_base_mid_lppc.tr, minor_us_mid_lppc.tr),
      # ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       #stars = FALSE,
       float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "minor_us_py" = "Peace Years",
                              "I(minor_us_py^2)" = "Peace Years Squared",
                              "I(minor_us_py^3)" = "Peace Years Cubed"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of Minor US Initiated MIDs",
       label = "table:appendix_minor_mids",
       custom.model.names = c("Base Unif", "Full Unif",
                              "Base Maj",  "Full Maj",
                              "Base LPPC",  "Full LPPC"),
       custom.note = "$^*$ 0 outside the confidence interval. 
       Peace year terms have been suppressed but are displayed in the appendix.
       Every model contains presidential fixed effects.", 
       sideways = TRUE)


#------------ Major US Initiated GML MIDs

texreg(list(major_us_base_mid_div.tr, major_us_mid_div.tr,
            major_us_base_mid_mjt.tr,  major_us_mid_mjt.tr,
            major_us_base_mid_lppc.tr, major_us_mid_lppc.tr),
     #  ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "major_us_py" = "Peace Years",
                              "I(major_us_py^2)" = "Peace Years Squared",
                              "I(major_us_py^3)" = "Peace Years Cubed"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of Major US Initiated MIDs",
       label = "table:appendix_major_mids",
       custom.model.names = c("Base Unif", "Full Unif",
                              "Base Maj", "Full Maj",
                              "Base LPPC", "Full LPPC"),
       custom.note = "$^*$ 0 outside the confidence interval. 
       Peace year terms have been suppressed but are displayed in the appendix.
       Every model contains presidential fixed effects.",
       sideways = TRUE
)


#------- Minor Uses of Force (Fordham & Sarver)

# Fixes a parsing error in one of the texreg objects 
# The updated values correspond to the values found in the tidy_tables 

minor_us_base_force_lppc.tr@ci.low[[11]] <- -.000273
minor_us_base_force_lppc.tr@ci.up[[11]] <- -.000294


texreg(list(minor_us_base_force_div.tr, minor_us_force_div.tr,
            minor_us_base_force_mjt.tr, minor_us_force_mjt.tr,
            minor_us_base_force_lppc.tr, minor_us_force_lppc.tr),
      # ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "minor_force_peace" = "Peace Years",
                              "I(minor_force_peace^2)" = "Peace Years Squared",
                              "I(minor_force_peace^3)" = "Peace Years Cubed"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of Minor US Force",
       label = "table:appendix_minor_us_force",
       custom.model.names = c("Base Unif", "Full Unif",
                              "Base Maj", "Full Maj",
                              "Base LPPC", "Full LPPC"),
       custom.note = "$^*$ 0 outside the confidence interval.
       All models contain presidential fixed effects and peace years.",
       sideways = TRUE)


#------- Major uses of Force (Fordham & Sarver)

texreg(list(major_us_base_force_div.tr, major_us_force_div.tr,
            major_us_base_force_mjt.tr, major_us_force_mjt.tr,
            major_us_base_force_lppc.tr, major_us_force_lppc.tr),
     #  ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "prezmajority"  = "Percent President Party",
                              "lppc" = "President Party Power",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "major_force_peace" = "Peace Years",
                              "I(major_force_peace^2)" = "Peace Years Squared",
                              "I(major_force_peace^3)" = "Peace Years Cubed"),
       # "(Intercept)" = "Intercept"),
       caption = "The Determinants of Major US Force",
       label = "table:appendix_major_us_force",
       custom.model.names = c("Base Unif", "Full Unif",
                              "Base Maj", "Full Maj",
                              "Base LPPC", "Full LPPC"),
       custom.note = "$^*$ 0 outside the confidence interval. 
       All models contain presidential fixed effects and peace years.", 
       sideways = TRUE)

#------ Pooled MIDs and Alternative Peace Year Specifications for Major/Minor MIDs

texreg(list(pooled_mid_base.tr, pooled_mid_full.tr,
            minor_mid_test_base.tr,minor_mid_test_full.tr,
            major_mid_test_base.tr, major_mid_test_full.tr),
   #    ci.force =  FALSE, ci.test = NULL, ci.force.level = 0.95, bold = 0.05, 
       stars = 0.05, float.pos = "h", dcolumn = FALSE, booktabs = TRUE, 
       use.packages = FALSE, single.row = FALSE, inline.css = TRUE, center = FALSE, 
       no.margin = FALSE,
       custom.coef.map = list("divided" = "Divided Government",
                              "approval_q1" = "Qtr. 1 Approval",
                              "election" = "Election Year",
                              "oversight_change" = "Oversight Change",
                              "democracy" = "Joint Democracy",
                              "cinc_proportion" = "CINC Ratio",
                              "clean_russ_inf" = "Active Russian Operation",
                              "us_onset_py" = "Peace Years",
                              "us_onset_py_2" = "Peace Years Squared",
                              "us_onset_py_3" = "Peace Years Cubed"),
       caption = "Pooled MIDs and Alternative Peace Years Specification",
       label = "table:appendix_pooled_mids",
       custom.model.names = c("Pooled MID 1", "Pooled MID 2",
                              "Minor MID 1", "Minor MID 2",
                              "Major MID 1", "Major MID 2"),
       custom.note = "$^*$ 0 outside the confidence interval.
            All models contain presidential fixed effects and peace years.", 
       sideways = TRUE)

